import csv
import json
import os


def csvToJson(csv_file_path, json_file_path):
    data = []
    # 定义需要保留的字段
    required_fields = ["talker", "room_name", "msg", "is_sender","type_name"]
    q_talker = None
    a_talker = None
    change_num = 0
    question = ""
    answer = ""

    # 打开CSV文件
    with open(csv_file_path, mode='r', newline='', encoding='utf-8') as csv_file:
        csv_reader = csv.DictReader(csv_file)
        # 记录可以存储该条数据的标记

        # 读取每一行
        for row in csv_reader:
            is_save = False
            # 过滤掉不需要的字段
            filtered_row = {key: row[key] for key in required_fields if key in row}

            if filtered_row["type_name"] != "文本":
                continue
            if not q_talker:
                q_talker = filtered_row['talker']
                if filtered_row['is_sender'] == 1:
                    continue
            if q_talker == filtered_row['talker'] and change_num == 0:
                question += filtered_row['msg']

            else:
                a_talker = filtered_row['talker']
                if a_talker != q_talker:
                    answer += filtered_row['msg']
                    change_num += 1
                    # print(change_num)
                else:
                    is_save = True

            # print(filtered_row)
            if q_talker and a_talker and question and answer and is_save:
                data_row = {
                    "instruction": question,
                    "input": "",
                    "output": answer
                }
                # print(data_row)
                data.append(data_row)
                q_talker = None
                a_talker = None
                question = ""
                answer = ""
                change_num = 0

    # 将处理后的数据写入JSON文件
    json_data = json.dumps(data, indent=4, ensure_ascii=False)
    with open(json_file_path, mode='w', encoding='utf-8') as json_file:
        json_file.write(json_data)

    print(f"CSV文件已成功转换为JSON文件，并保存到{json_file_path}")


def convert_all_csv_to_json(folder_path):
    # 获取最外层父级目录路径
    parent_folder_path = os.path.dirname(folder_path)
    # 使用os.walk递归遍历文件夹及其子文件夹
    for root, dirs, files in os.walk(folder_path):
        for file_name in files:
            if file_name.endswith(".csv"):
                # 获取CSV文件的完整路径
                csv_file_path = os.path.join(root, file_name)
                # 使用文件名作为JSON文件的前缀，并将JSON文件存储到父级目录中
                json_file_name = f"{os.path.splitext(file_name)[0]}a.json"
                json_file_path = os.path.join(parent_folder_path, "save", json_file_name)
                # 调用转换函数
                csvToJson(csv_file_path, json_file_path)
                print(f"已转换：{csv_file_path} -> {json_file_path}")
def read_and_merge_json(folder_path):
    merged_data = []  # 用来存储合并后的数据
    # 遍历文件夹中的所有文件
    for file_name in os.listdir(folder_path):
        if file_name.endswith("a.json"):
            json_file_path = os.path.join(folder_path, file_name)

            # 读取 JSON 文件
            with open(json_file_path, mode='r', encoding='utf-8') as json_file:
                data = json.load(json_file)
                # 假设每个 JSON 文件的内容是一个列表，将其添加到 merged_data 中
                merged_data.extend(data)  # 合并到列表中

    return merged_data

def save_merged_json(data, output_file):
    with open(output_file, mode='w', encoding='utf-8') as json_file:
        json.dump(data, json_file, indent=4, ensure_ascii=False)

if __name__ == '__main__':
    convert_all_csv_to_json("WeChatDataSet")
    # 示例：读取文件夹中的所有 JSON 文件并合并
    folder_path = 'save'  # 替换为你存储 JSON 文件的文件夹路径
    merged_data = read_and_merge_json(folder_path)
    # 将合并后的数据保存为一个新的 JSON 文件
    output_file = '12_09.json'  # 合并后的 JSON 文件名
    save_merged_json(merged_data, output_file)